package com.topboya.linker.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.OrgLevel0Item;
import com.topboya.linker.model.response.CustomerByOrgIdAndNewlyCusListResopnse;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.NewlyOrgListResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.adapter.CustomerAdapter;
import com.topboya.linker.ui.adapter.CustomerExpandableItemAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CustomerPresenter;
import com.topboya.linker.ui.view.CustomerView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomerActivity extends BaseActivity<CustomerView, CustomerPresenter> implements CustomerView {

    private static final String TAG = "CustomerActivity-";
    private SmartRefreshLayout smartrefreshlayout;
    private RecyclerView recycle_coustomer;
    private CustomerAdapter customerAdapter;
//    private ImageView img_add;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_customer;
    }

    @Override
    protected void initView() {

        smartrefreshlayout = findViewById(R.id.smart_refreshlayout);
        recycle_coustomer = findViewById(R.id.recycle_coustomer);
        smartrefreshlayout.setEnableRefresh(false);//禁止刷新
//        img_add = findViewById(R.id.img_add);
    }

    @Override
    protected void initData() {

         mPresenter.getOrgList("1",null);
    }

    @Override
    protected void initListener() {

//        img_add.setOnClickListener(v -> jumpToActivity(AddDeviceActivity.class));
        smartrefreshlayout.setOnLoadmoreListener(refreshlayout -> {

//            customerAdapter.notifyDataSetChanged();
            refreshlayout.finishLoadmore();
        });

    }

    @Override
    protected CustomerPresenter createPresenter() {

        return new CustomerPresenter(this);
    }

    private int postion =0;
    @Override
    public void setCustomeData(ArrayList<OrganizationResopnse.DocDetailInfoList> groupList,ArrayList<NewlyOrgListResponse.Data> groupHListNewly) {

        ArrayList<MultiItemEntity>  orglistitem1 =  generateData(groupList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);

        // 创建适配器
        CustomerExpandableItemAdapter customerExpandableItemAdapter = new CustomerExpandableItemAdapter(orglistitem1,this,groupList);
        recycle_coustomer.setLayoutManager(layoutManager);
        recycle_coustomer.setAdapter(customerExpandableItemAdapter);



    }



    @Override
    public void setDepartment(List<CustomerByOrgIdResponse.DepartmentList> departmentlist) {

        LogUtils.v(TAG +"setDepartment()");

    }

    @Override
    public void setDictByKey(List<DictByKeyResponse.Data> data) {

    }

    @Override
    public void createCustomer() {


    }

    @Override
    public void setDepartVisitor(HashMap<String, List<CustomerByOrgIdAndNewlyCusListResopnse.CusListChild>> departmentgroupmap, List<CustomerByOrgIdAndNewlyCusListResopnse.NewlyCusList> newlyCusLists) {

    }



    private ArrayList<MultiItemEntity> generateData(ArrayList<OrganizationResopnse.DocDetailInfoList> groupList) {

        ArrayList<MultiItemEntity> res = new ArrayList<>();

        for (int i = 0; i < groupList.size(); i++) {

            OrgLevel0Item lv0 = new OrgLevel0Item();
            lv0.setHaspatialname(groupList.get(i).getOrgName());
            lv0.setGetHaspatialevel(groupList.get(i).getLevelName());
            lv0.setOrgID(groupList.get(i).getId());
            res.add(lv0);
        }
        return res;
    }
}
